import { defineStore } from 'pinia'
import Api from '@/api'
import Dict from '@/Utils/dict'
import { sessionGet } from '@/Utils/storage'
export const useHomeStore = defineStore('home', {
  persist: {
    enabled: true,
  },
  state: () => {
    return {
      //业绩目标达成信息
      globalCompletedReq: {
        title: '',
        info: [],
      },
      //国家业绩目标达成信息
      countryCompletedReq: {
        title: '',
        info: [],
      },
      //全球新会员增长
      globalNewMemberReq: {
        title: '',
        info: [],
      },
      //全球业绩增长
      globalPerformanceReq: {
        title: '',
        info: [],
      },
      // 性别占比
      homeSexPie: {
        title: '',
        info: [],
      },
      // 年龄占比
      homeAgePie: {
        title: '',
        info: [],
      },
      // 新老会员业绩占比
      homeNewOldPie: {
        title: '',
        info: [],
      },
      // 零售业绩与拓展业绩占比
      homeRetailPie: {
        title: '',
        info: [],
      },
      // 各大区业绩占比
      homeAreaPie: {
        title: '',
        info: [],
      },
      // 全球会员数
      homeFlexLeft: {
        info: []
      },
      // 各区有效会员
      homeGlobalValidMembers: {
        info: [],
        sub_titles: [],
        title: '',
        data_month:''
      },
      // 今日成交信息
      homeTodayMoney: {
        title: '',
        suffix: '',
        amount: '',
      },
      // 业绩同比
      homeGlobalPerformanceYoY: {
        title: '',
        info: [],
        sub_titles: []
      },
      // 业绩环比
      homeGlobalPerformanceRatio: {
        title: '',
        info: [],
        sub_titles: [],
      },
      // 业绩金额
      homeFlexRight: {
        amount: '',
        suffix: '',
        title: ''
      },
      // 会员级别
      homeGlobalMemberLevelCounts: {
        info: [],
        sub_titles: [],
        title: ''
      },
      // 国家业绩同比
      homeCountryPerformanceYoY: {
        title: "",
        info: []
      },
      // 国家业绩环比
      homeNationalPerformanceMoM: {
        title: '',
        info: []
      },
      // 各区经销商
      homeGlobalCoreDealers: {
        info: [],
        title: ''
      },
      // 核心经销商表格
      homeTableCoreDealers: {
        info: [],
        title: ''
      },
      // 版本
      version: {}
    }
  },

  actions: {
    clearVersion() {
      this.version = {}
    },
    getAll() {
      const businessLevel = sessionGet('role')?.businessLevel || 0
      if (!businessLevel) {
        return ''
      }
      // Dict对象获取home,然后key和value对调,后端那个key为键值,咱们的方法为值
      const overturnDict = Object.keys(Dict.home).reduce(
        (obj: any, key: any) => {
          obj[Dict.home[key]] = key
          return obj
        },
        {}
      )

      Api.homeGetAll().then((res: any) => {
        if (res.code != 0) {
          return false
        }
        const newVersion = res.data
        // 获取到版本号newVersion和本地版本号version对比,如果值不一样,获取到key
        const keys = Object.keys(newVersion).filter((item: any) => {
          if (!item) {
            return false
          }
          // 本地version和newVersion对比,如果值不一样,就返回key
          const localVersionKey = overturnDict[item] // 本地的版本的key
          if (!localVersionKey) {
            return false
          }
          const localVersion = this.version[localVersionKey] || ''
          return newVersion[item] !== localVersion
        })
        // 如果key有值,就调取接口
        if (keys.length) {
          keys.forEach((item: any) => {
            const localVersionKey = overturnDict[item] // 本地的版本的key
            if (localVersionKey) {
              try {
                Api[localVersionKey]().then((res: any) => {
                  if (res.code == 0 || res.code == 10000) {
                    this[localVersionKey] = res.data
                    this.version[localVersionKey] = newVersion[item]
                  }
                })
              } catch (e) {
                console.log('err', e)
              }
            }
          })
        }
      })
    },
  },
})
